package cn.cocho.custom.yunCashierDf.agency.service;

import cn.cocho.common.web.character.StringUtils;
import cn.cocho.common.web.jqgrid.JQGridPage;
import cn.cocho.common.web.jqgrid.JQGridUtils;
import cn.cocho.custom.yunCashierDf.agency.domain.AgencyInfo;
import cn.cocho.dborm.core.Dborm;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by WQJ on 2017/5/22.
 */
@Service("agencyInfoService")
public class AgencyInfoService {

    public JQGridPage getListByJQgrid(JQGridPage<AgencyInfo> pageJQGrid) {
        pageJQGrid = JQGridUtils.getListByJQgrid(pageJQGrid, AgencyInfo.class, "agency_info");
        for (AgencyInfo agencyInfo : pageJQGrid.getDataRows()) {
             agencyInfo.init();
        }
        return pageJQGrid;
    }

    public void update(AgencyInfo agencyInfo) {
        Dborm.replace(agencyInfo);
    }

    public void add(AgencyInfo agencyInfo) {
        agencyInfo.setAgencyId(StringUtils.getUUID());
        Dborm.insert(agencyInfo);
    }

    public AgencyInfo getAgencyInfoById(String agencyId) {
        AgencyInfo agencyInfo = new AgencyInfo();
        agencyInfo.setAgencyId(agencyId);
        return Dborm.getEntityByExample(agencyInfo, true);
    }

    public boolean insertAgencyInfo(AgencyInfo agencyInfo) {
        agencyInfo.setAgencyId(StringUtils.getUUID());
        return Dborm.insert(agencyInfo);
    }

    public boolean replaceAgencyInfo(AgencyInfo agencyInfo) {
        agencyInfo.setAgencyId(agencyInfo.getAgencyId());
        return Dborm.replace(agencyInfo);
    }

    public List<AgencyInfo> getAgencyInfo() {
        String sql = "SELECT * FROM agency_info ORDER BY agency_id ASC";
        return Dborm.getEntities(sql, null, AgencyInfo.class);
    }
}
